package br.edu.ufersa.scrumgame.modelo;

import java.util.Arrays;

import javax.persistence.*;

import br.edu.ufersa.scrumgame.modelo.sistema.Dia;

@Entity
@NamedQueries({
	@NamedQuery(name = "Jogador.findByEmail", query = "select u from Jogador u where u.email = :email"),
	@NamedQuery(name = "Jogador.findAllOrderExp", query = "select u from Jogador u where u.ativo = true and u.acesso = 'JOGADOR' order by u.experiencia desc")
})
public class Jogador extends Usuario {
	
	private String nome;
	
	private double scrums;
	
	private int experiencia;
	
	private int energia;
	
	@ManyToOne
	@JoinColumn(name = "id_diaInicio")
	private Dia diaInicio;
	
	private byte[] foto;
	
	@ManyToOne
	@JoinColumn(name = "id_nivel")
	private Nivel nivel;
	
	@ManyToOne
	@JoinColumn(name = "id_papel")
	private Papel papel;
	
	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public double getScrums() {
		return scrums;
	}

	public void setScrums(double scrums) {
		this.scrums = scrums;
	}

	public int getExperiencia() {
		return experiencia;
	}

	public void setExperiencia(int experiencia) {
		this.experiencia = experiencia;
	}

	public int getEnergia() {
		return energia;
	}

	public void setEnergia(int energia) {
		this.energia = energia;
	}

	public Nivel getNivel() {
		return nivel;
	}

	public void setNivel(Nivel nivel) {
		this.nivel = nivel;
	}

	public Papel getPapel() {
		return papel;
	}

	public void setPapel(Papel papel) {
		this.papel = papel;
	}

	public byte[] getFoto() {
		return foto;
	}

	public void setFoto(byte[] foto) {
		this.foto = foto;
	}

	public Dia getDiaInicio() {
		return diaInicio;
	}

	public void setDiaInicio(Dia diaInicio) {
		this.diaInicio = diaInicio;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((diaInicio == null) ? 0 : diaInicio.hashCode());
		result = prime * result + energia;
		result = prime * result + experiencia;
		result = prime * result + Arrays.hashCode(foto);
		result = prime * result + ((nivel == null) ? 0 : nivel.hashCode());
		result = prime * result + ((nome == null) ? 0 : nome.hashCode());
		result = prime * result + ((papel == null) ? 0 : papel.hashCode());
		long temp;
		temp = Double.doubleToLongBits(scrums);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Jogador other = (Jogador) obj;
		if (diaInicio == null) {
			if (other.diaInicio != null)
				return false;
		} else if (!diaInicio.equals(other.diaInicio))
			return false;
		if (energia != other.energia)
			return false;
		if (experiencia != other.experiencia)
			return false;
		if (!Arrays.equals(foto, other.foto))
			return false;
		if (nivel == null) {
			if (other.nivel != null)
				return false;
		} else if (!nivel.equals(other.nivel))
			return false;
		if (nome == null) {
			if (other.nome != null)
				return false;
		} else if (!nome.equals(other.nome))
			return false;
		if (papel == null) {
			if (other.papel != null)
				return false;
		} else if (!papel.equals(other.papel))
			return false;
		if (Double.doubleToLongBits(scrums) != Double.doubleToLongBits(other.scrums))
			return false;
		return true;
	}

	public void subScrums(double scrumsCompra) {
		scrums -= scrumsCompra;
	}

	public void sumEnergia(int ganhoEnergia) {
		energia += ganhoEnergia;
	}

	public void sumExperiencia(int ganhoExperiencia) {
		experiencia += ganhoExperiencia;		
	}

	public void sumScrums(double ganhoScrums) {
		scrums += ganhoScrums;
	}

	public void subEnergia(int perdaEnergia) {
		energia -= perdaEnergia;
	}

}
